package com.insurance.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.insurance.domain.bo.*;
import com.insurance.domain.entity.UserProduct;
import com.insurance.service.ChainConnectService;
import com.insurance.service.impl.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ResponseBody
@Service
@Resource
public class ChainConnectServiceImpl implements ChainConnectService {

    @Autowired
    private HttpUtils httpUtils;

    public JSONObject TYQX(CPatient cPatient){
        //定义一个列表类型;
        List<String> funParam = new ArrayList<String>();

        funParam.add(cPatient.getAddress());//用户地址，公钥

        //String All;

        //私钥。名字，年龄。性别，描述;

        funParam.add(cPatient.getPirvateKey());
        funParam.add(cPatient.getName());
        funParam.add(cPatient.getAge());
        funParam.add(cPatient.getSex());
        funParam.add(cPatient.getMessage());

        JSONObject result = httpUtils.writeContract("TYQX", funParam);
        return result;
    }
    //
    public JSONArray QueryPat(CPatient cPatient){
        //定义一个列表类型;
        List<String> funParam = new ArrayList<>();
        funParam.add(cPatient.getAddress());
        JSONArray result = httpUtils.AllwriteContract("QueryPat", funParam);
        return result;
    }
    //
    public JSONArray QueryAllEvi(CPatient cPatient){
        //定义一个列表类型;
        List<String> funParam = new ArrayList<>();
        funParam.add(cPatient.getAddress());
        JSONArray result = httpUtils.AllwriteContract("QueryAllEvi", funParam);
        return result;
    }
    //
    public JSONArray QueryAllUsers(){
        //定义一个列表类型;
        List<String> funParam = new ArrayList<>();
//        funParam.add(queryAllEvi.getPaterAdd());
//        System.out.println(funParam);//输出参数看看有没有传进去:
        JSONArray result = httpUtils.readContract("QueryAllUsers");
        return result;
    }
    //
    public JSONObject WriteMessaage(CPatient cPatient){
        //定义一个列表类型;
        List<String> funParam = new ArrayList<>();
        funParam.add(cPatient.getAddress());
        funParam.add(cPatient.getMessage());
        System.out.println(funParam);//输出参数看看有没有传进去:
        JSONObject result = httpUtils.writeContract("WriteMessaage",funParam);//Contract("WriteMessaage");
        System.out.println(result);
        return result;
    }
//    //
//    public JSONArray SetXiEvi(SetXiEvi setXiEvi){
//        //定义一个列表类型;
//        List<String> funParam = new ArrayList<>();
//        funParam.add(setXiEvi.getUser());
//        funParam.add(setXiEvi.getAdd());
//        String X;
//// X=String(setXiEvi.getX());
//
////        funParam.add(setXiEvi.getX());
//
//        System.out.println(funParam);//输出参数看看有没有传进去:
//        JSONArray result = HttpUtils.readContract("WriteMessaage");//Contract("WriteMessaage");
//        System.out.println(result);
//        return result;
//    }
    public JSONObject Verification(CDoctor cDoctor){

        List<String> funParam = new ArrayList<>();
        funParam.add(cDoctor.getDocAdd());
        funParam.add(cDoctor.getPatAdd());
        JSONObject result = httpUtils.writeContract("Verification",funParam);//Contract("WriteMessaage");//Contract("WriteMessaage");
        return result;
    }
    //
    public JSONArray QueryPeople(CDoctor  cDoctor){

        List<String> funParam = new ArrayList<>();
        funParam.add(cDoctor.getDocAdd());
        JSONArray result = httpUtils.AllwriteContract("QueryPeople",funParam);//Contract("WriteMessaage");//Contract("WriteMessaage");
        return result;
    }
    //
    public JSONObject WrtieEviCode(WrtieEviCode wrtieEviCode){
//
        List<String> funParam = new ArrayList<>();
        funParam.add(wrtieEviCode.getDocAdd());
        funParam.add(wrtieEviCode.getPatAdd());
        funParam.add(wrtieEviCode.getMedical());
        funParam.add(wrtieEviCode.getAdvice());
        funParam.add(wrtieEviCode.getSymptoms());
        funParam.add(wrtieEviCode.getTemperature());
        funParam.add(wrtieEviCode.getHospital());
//
        JSONObject result = httpUtils.writeContract("WrtieEviCode",funParam);//Contract("WriteMessaage");//Contract("WriteMessaage");
        return result;
    }
    //
    public JSONObject OutHospital(CDoctor cDoctor){

        List<String> funParam = new ArrayList<>();
        funParam.add(cDoctor.getDocAdd());
        funParam.add(cDoctor.getPatAdd());
        JSONObject result = httpUtils.writeContract("OutHospital",funParam);//Contract("WriteMessaage");//Contract("WriteMessaage");
        return result;
    }
    //QueryGX
    public ArrayList<Case> QueryGX(SharedRecord sharedRecord){

        List<String> funParam = new ArrayList<>();
        funParam.add(sharedRecord.getPatAdd());
        JSONArray result = httpUtils.AllwriteContract("QueryGX",funParam);//Contract("WriteMessaage");//Contract("WriteMessaage");
        ArrayList<Case> cases = new ArrayList<>();
        Iterator<Object> iterator = result.stream().iterator();
        while (iterator.hasNext()){
            JSONArray next = (JSONArray) iterator.next();
            Iterator<Object> iterator1 = next.stream().iterator();
            while (iterator1.hasNext()){
                Case case1 = new Case();
                case1.setCaseId(String.valueOf(iterator1.next()));
                case1.setMedical(String.valueOf(iterator1.next()));
                case1.setAdvice(String.valueOf(iterator1.next()));
                case1.setSymptoms(String.valueOf(iterator1.next()));
                case1.setTemp(String.valueOf(iterator1.next()));
                case1.setHospital(String.valueOf(iterator1.next()));
                cases.add(case1);
            }
        }
        return cases;
    }
    //QueryGL
    public ArrayList<Case> QueryGL(SharedRecord sharedRecord){

        List<String> funParam = new ArrayList<>();
        funParam.add(sharedRecord.getPatAdd());
        JSONArray result = httpUtils.AllwriteContract("QueryGL",funParam);//Contract("WriteMessaage");//Contract("WriteMessaage");
        ArrayList<Case> cases = new ArrayList<>();
        Iterator<Object> iterator = result.stream().iterator();
        while (iterator.hasNext()){
            JSONArray next = (JSONArray) iterator.next();
            Iterator<Object> iterator1 = next.stream().iterator();
            while (iterator1.hasNext()){
                Case case1 = new Case();
                case1.setCaseId(String.valueOf(iterator1.next()));
                case1.setMedical(String.valueOf(iterator1.next()));
                case1.setAdvice(String.valueOf(iterator1.next()));
                case1.setSymptoms(String.valueOf(iterator1.next()));
                case1.setTemp(String.valueOf(iterator1.next()));
                case1.setHospital(String.valueOf(iterator1.next()));
                cases.add(case1);
            }
        }
        return cases;
    }
    //GXGL
    public JSONObject GXGL(GXGL gxgl){

        List<String> funParam = new ArrayList<>();
        funParam.add(gxgl.getQueriedPatAdd());
        funParam.add(gxgl.getCaseId());
        JSONObject result = httpUtils.writeContract("GXGL",funParam);//Contract("WriteMessaage");//Contract("WriteMessaage");
        return result;
    }
    //QueryBlockChain
    public ArrayList<Case> QueryBlockChain(){

        List<String> funParam = new ArrayList<>();
        JSONArray result = httpUtils.readContract("QueryBlockChain");//Contract("WriteMessaage");//Contract("WriteMessaage");
        ArrayList<Case> cases = new ArrayList<>();
        Iterator<Object> iterator = result.stream().iterator();
        while (iterator.hasNext()){
            JSONArray next = (JSONArray) iterator.next();
            Iterator<Object> iterator1 = next.stream().iterator();
            while (iterator1.hasNext()){
                Case case1 = new Case();
                case1.setCaseId(String.valueOf(iterator1.next()));
                case1.setMedical(String.valueOf(iterator1.next()));
                case1.setAdvice(String.valueOf(iterator1.next()));
                case1.setSymptoms(String.valueOf(iterator1.next()));
                case1.setTemp(String.valueOf(iterator1.next()));
                case1.setHospital(String.valueOf(iterator1.next()));
                cases.add(case1);
            }
        }
        return cases;
    }

    /**
     * 设置权限
     * @param sharedRecord
     * @return
     */
    public JSONObject SetXiEvi(SharedRecord sharedRecord) {
        ArrayList<Object> funParam = new ArrayList<>();
        funParam.add(sharedRecord.getQueriedPat());
        funParam.add(sharedRecord.getPatAdd());
        funParam.add(sharedRecord.getPermission());
        JSONObject result=httpUtils.writeContract("SetXiEvi",funParam);
        return result;
    }

    /**
     * 创建保险自动索赔约束
     * @param userProduct
     * @return
     */
    @Override
    public JSONObject CreateReason(UserProduct userProduct) {
        //定义一个列表类型;
        List<String> funParam = new ArrayList<String>();
        funParam.add(userProduct.getProductId().toString());
        funParam.add(userProduct.getConstraint1());
        funParam.add(userProduct.getConstraint2());
        funParam.add(userProduct.getConstraint3());
        funParam.add(userProduct.getConstraint4());
        funParam.add(userProduct.getConstraint5());
        //最多五个满足1个条件即可判断可以进行自动索赔操作;
        System.out.println(funParam);//输出参数看看有没有传进去:
        JSONObject result = httpUtils.writeContract("CreateReason", funParam);
        System.out.println(result);
        return result;
    }

    /**
     * 购买保险
     * @param userProduct
     * @return
     */
    @Override
    public JSONObject BuyInsurance(UserProduct userProduct) {
        List<String> funParam = new ArrayList<String>();
        funParam.add(userProduct.getUsername());//用户地址
        funParam.add(userProduct.getProductId().toString());//保险的ID;
        JSONObject result=httpUtils.writeContract("BuyInsurance",funParam);
        JSONObject jsonObject = CreateReason(userProduct);
        return jsonObject;
    }

    /**
     * 自动索赔
     * @param userProduct
     * @return
     */
    @Override
    public JSONArray AutoMoney(UserProduct userProduct) {
        List<String> funParam = new ArrayList<String>();
        funParam.add(userProduct.getUsername());//用户地址
        System.out.println(funParam);
        JSONArray result=httpUtils.AllwriteContract("AutoMoney",funParam);
        return result;
    }


}
